﻿(function () {
    var myApp = angular.module('MyControllers');

    myApp.controller('Examinee', ['$scope', '$http', '$modal', 'ExamineeService', function ($scope, $http, $modal, service) {
        var srv = service.list;
        $scope.planType = 0;
        $scope.hasMock = false;

        $scope.getList = function () {
            $scope.loadingState = true;

            srv.getList().success(function (data) {
                $scope.formalList = data.formalList;
                $scope.mockList = data.mockList;

                angular.forEach($scope.formalList, function (item) {
                    var statusInfo = $scope.status(item.examMode, item.sysState);

                    item.className = statusInfo.className;
                    item.buttonName = statusInfo.buttonName;
                    item.showName = statusInfo.showName;
                });
                angular.forEach($scope.mockList, function (item) {
                    var statusInfo = $scope.status(item.examMode, item.sysState);

                    item.className = statusInfo.className;
                    item.buttonName = statusInfo.buttonName;
                    item.showName = statusInfo.showName;
                });

                if (!$scope.formalList || $scope.formalList.length <= 0) {
                    $scope.planType = 1;
                    $scope.list = $scope.mockList;
                }
                if (!$scope.mockList || $scope.mockList.length <= 0) {
                    $scope.planType = 0;
                    $scope.list = $scope.formalList;
                } else {
                    $scope.hasMock = true;
                }

                $scope.loadingState = false;
            });
        };

        $scope.official = function () {
            $scope.planType = 0;
            $scope.list = $scope.formalList;
        };

        $scope.imatate = function () {
            $scope.planType = 1;
            $scope.list = $scope.mockList;
        };

        $scope.status = function (examMode, sysState) {
            var className, buttonName, showName;

            if (examMode == 1) {
                switch (sysState) {
                    case 0:
                        className = 'nostart';
                        buttonName = '预览试卷';
                        showName = '未开始';
                        break;
                    case 1:
                        className = 'noanswer';
                        buttonName = '开始考试';
                        showName = '未作答';
                        break;
                    case 2:
                        className = 'goingon';
                        buttonName = '继续答题';
                        showName = '进行中';
                        break;
                }
            } else {
                if (sysState == 2) {
                    className = 'goingon';
                    buttonName = '继续答题';
                    showName = '进行中';
                } else {
                    className = 'noanswer';
                    buttonName = '开始考试';
                    showName = '未作答';
                }
            }

            return { className: className, buttonName: buttonName, showName: showName };
        }

        $scope.download = function (model) {
            srv.isSignSubject(model).success(function (data) {
                if (data) {
                    window.location.href = 'DownloadPaper'
                        + '?token=' + Token
                        + '&planId=' + model.examPlanInfoID
                        + '&appId=' + model.appID
                        + '&subjectId=' + model.examSubjectID;
                } else {
                    alert('未签到不能进行考试！');
                }
            });
        }

        $scope.getList();
    }]);

    myApp.controller('noticeExam', ['$scope', '$http', '$modal', 'ExamineeService', function ($scope, $http, $modal, service) {
        var notice = service.notice;
        $scope.countDown = function (isPage) {
            $scope.loadingState = true;

            if (!isPage) $scope.current = 1;
            notice.getCountDown().success(function (data) {
                $scope.timeSeconds = parseInt(data);
            });

            notice.getTime().success(function (data) {
                $scope.timeToBegin = parseInt(data);
            });
        };

        $scope.getTemplate = function (isPage) {
            $scope.loadingState = true;
            if (!isPage) $scope.current = 1;
            notice.getJsonTemplate().success(function (data) {
                $scope.sections = data.sectionTemplateCollection;
            });
        };

        $scope.color = 'btn-n';
        $scope.fun = function () {
            if (($scope.timeToBegin * 60) > $scope.timeSeconds) {
                $scope.color = 'btn-1';
            }
        }

        $scope.url = function () {
            if (($scope.timeToBegin * 60) > $scope.timeSeconds) {

                window.location.href = $scope.timeSeconds < 10
                    ? 'ExamPaper?token=' + Token
                    : 'Respondence?token=' + Token;
            }
        };

        var setCountDown = function () {
            $scope.dd = parseInt($scope.timeSeconds / 60 / 60 / 24, 10);//计算剩余的天数
            $scope.hh = parseInt(($scope.timeSeconds / 60 / 60) % 24, 10);//计算剩余的小时数  
            $scope.mm = parseInt(($scope.timeSeconds / 60) % 60, 10);//计算剩余的分钟数  
            $scope.ss = parseInt($scope.timeSeconds % 60, 10);//计算剩余的秒数 
            --$scope.timeSeconds;
            if ($scope.timeSeconds < 0) {
                clearInterval(timer);
                window.location.href = 'ExamPaper?token=' + Token;
            }
        };

        var timer = setInterval(function () {
            $scope.fun();
            $scope.$apply(setCountDown);
        }, 1000);

        $scope.countDown();
        $scope.getTemplate();
    }]);
})();